<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeshowing">
    <!-- 表单 -->
    <el-form ref="deFrom" label-width="120px" :model="from" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="from.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="from.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="from.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="from.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in truelistes" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="from.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="from.departmentName" style="width:50%" placeholder="请选择部门" @click.native.stop="focusList" />
        <div v-if="showtree" class="tree-box">
          <el-tree v-loading="loading" :data="bumenList" :props="{label:'name'}" @node-click="clickList" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="from.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button>取消</el-button>
      <el-button type="primary" @click="adduserclick">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import truelist from '@/constant/employees'
import { truesListData } from '@/utils/index'
import { DeactivatedAPI } from '@/api/departments'
import { AdduserList } from '@/api/empoly'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      bumenList: [],
      from: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      showtree: false,
      loading: false,
      truelistes: truelist.hireType,
      rules: {
        username: [{ required: true, message: '名字不能为空', tirgger: 'blur' },
          { min: 1, max: 4, message: '1-4位字符', tirgger: 'blur' }
        ],
        mobile: [{ required: true, message: '手机号不能为空', tirgger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', tirgger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空' }],
        workNumber: [{ required: true, message: '工号不能为空', tirgger: 'blur' },
          { min: 1, max: 6, message: '1-6位字符', tirgger: 'blur' }
        ],
        departmentName: [{ required: true, message: '部门不能为空', tirgger: 'blur' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空' }],
        correctionTime: [{ required: true, message: '转正时间不能为空' }]
      }
    }
  },
  methods: {
    /**
     * 点击关闭添加弹窗
     */
    closeshowing() {
      this.$emit('update:showDialog', false)
      this.$refs.deFrom.resetFields()
    },
    /**
     * 获取部门列表数据
     */
    async focusList() {
      console.log(this.truelistes)
      this.showtree = !this.showtree
      this.loading = true
      const { data: res } = await DeactivatedAPI()
      // console.log(res)
      // 将数据装换为树形结构
      this.loading = false
      this.bumenList = truesListData(res.depts, '')
      console.log(this.bumenList)
    },
    /**
     * 通过eml自身事件点击获取数据赋值
     */
    clickList(data) {
      console.log(data)
      if (data.children && data.children.length > 0) return
      this.from.departmentName = data.name
      this.showtree = false
    },
    /**
     * 兜底校验，添加员工
     */
    adduserclick() {
      this.$refs.deFrom.validate(async(vaild) => {
        if (!vaild) return
        await AdduserList(this.from)
        this.$message.success('添加成功')
        this.closeshowing()
        this.$parent.UserListAPI()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
